package com.game.utils
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class MyTimer
	{
		private var _times:int
		private var _timer:Timer;
		private var _fun:Function;
		/**
		 * timer
		 */
		public function MyTimer()
		{
			
		}
		/**
		 * 
		 * @param fun() 回调函数
		 * @param delay 间隔时间(毫秒)
		 * @param timer 次数,默认无限次
		 */
		public function setTimer(fun:Function,delay:uint,times:uint=0):void{
			gc();
			_fun=fun;
			_times=times;
			if(_times==0){
				_times=-1;
			}
			_timer=new Timer(delay,times);
			_timer.addEventListener(TimerEvent.TIMER,handleTimer);
			_timer.start();
		}
		
		private function handleTimer(e:TimerEvent):void{
			_fun();
			if(_times!=-1){
				_times--;
				if(_times==0){
					gc();
				}
			}
			
		}
		/**
		 * 清理timer
		 */
		public function gc():void{
			if(_timer==null){
				return;
			}
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER,handleTimer);
			_timer=null;
			_fun=null;
		}
		
	}
}